<?php
    require_once 'config.php';
	require_once 'Date.php';
	require_once 'header.php';

    if(!isset($_REQUEST['type']) || empty($_REQUEST['type'])) {
        $_REQUEST['type'] = "cat";
    }

    $type = $_REQUEST['type'];

    if(!empty($_REQUEST["month"])) {
	    $m = (int)$_REQUEST["month"];
    }
    else {
	    $m = date('m');
    }

	$m = sprintf("%02d", $m);

	if(!empty($_REQUEST["year"])) {
	    $y = (int)$_REQUEST["year"];
    }
    else {
        $y = date('Y');
    }

	if(!empty($_REQUEST["date"])) {
	    $d = (int)$_REQUEST["date"];
    }
    else {
        $d = date('d');
    }

    $d = sprintf("%02d", $d);

	if(!isset($_REQUEST['duration'])) {
		$_REQUEST['duration'] = "monthly";
	}

	$cats = getCategory();

	$catexp = array();

	if($_REQUEST['duration'] == 'monthly') {
	    $sum = getTotalExpenseForMonth($y, $m, $_SESSION['userid']);
		if(is_array($cats)) {
			foreach($cats as $cat) {
		    	$catexp[$cat['category_name']] = array();
			    $catexp[$cat['category_name']][0] = getCategoryExpenseForMonth($y, $m, $cat['category_id'], $_SESSION['userid']);
			    $catexp[$cat['category_name']][1] = $cat['category_color'];
	        }
        }
?>
        <h4>Total Expenses by Category for the month of <?= $Months[(int)$m] ?>, <?= $y ?></h4>
<?php
    }
    else if($_REQUEST['duration'] == 'weekly') {
        $sum = getWeekData($m, $d, $y, $_SESSION['userid']);
        if(is_array($cats)) {
            foreach($cats as $cat) {
                $catexp[$cat['category_name']] = array();
                $catexp[$cat['category_name']][0] = getWeekData($m, $d, $y, $_SESSION['userid'], $cat['category_id']);
                $catexp[$cat['category_name']][1] = $cat['category_color'];
            }
        }
?>
        <h4>Total Expenses by Category for the week of <?= $Months[(int)$m] ?> <?= $d ?>, <?= $y ?></h4>
<?php
    }
    else if($_REQUEST["duration"] == "yearly") {
        $sum = getYearData($y, $_SESSION['userid']);
        if(is_array($cats)) {
            foreach($cats as $cat) {
                $catexp[$cat['category_name']] = array();
                $catexp[$cat['category_name']][0] = getYearData($y, $_SESSION['userid'], $cat['category_id']);
                $catexp[$cat['category_name']][1] = $cat['category_color'];
            }
        }
?>
        <h4>Total Expenses by Category for the Year <?= $y ?></h4>
<?php
    }
    if($sum > 0) {
?>
<img src="mychart.php?data=<?= urlencode(serialize($catexp)) ?>&dummy=<?= time() ?>">
<?php
	}
	else {
		echo "<p>No expenses found.</p>";
	}
?>
</div>
<p align="center">
<a href="showgraph.php?type=<?= $type ?>&duration=weekly&year=<?= $y ?>&month=<?= $m ?>&date=<?= $d ?>" title="Expenses by Category for This Week">This Week</a>
|
<a href="showgraph.php?type=<?= $type ?>&duration=monthly&year=<?= $y ?>&month=<?= $m ?>&date=<?= $d ?>" title="Expenses by Category for This Month">This Month</a>
|
<a href="showgraph.php?type=<?= $type ?>&duration=yearly&year=<?= $y ?>&month=<?= $m ?>&date=<?= $d ?>" title="Expenses by Category for This Year">This Year</a>
</p>
</body>
</html>
